package com.easy.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Goods;
import com.easy.common.service.IGoodsService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    
    @Autowired
    IGoodsService goodsService;

    // 根据id获取商品信息
    @GetMapping("/id/{id}")
    public Result getGoodsById(@PathVariable Integer id) {
        try {
            return Result.success(goodsService.getGoodsById(id));
        } catch (Exception e) {
            return Result.fail(500, "获取商品信息失败: " + e.getMessage());
        }
    }

    // 根据名称获取商品信息
    @GetMapping("/name/{name}")
    public Result getGoodsByName(@PathVariable String name) {
        try {
            return Result.success(goodsService.getGoodsByName(name));
        } catch (Exception e) {
            return Result.fail(500, "获取商品信息失败: " + e.getMessage());
        }
    }

    // 添加商品
    @PostMapping("/add")
    public Result addGoods(@RequestBody Goods goods) {
        try {
            int result = goodsService.addGoods(goods);
            if (result > 0) {
                return Result.success("商品添加成功");
            } else {
                return Result.fail(500, "商品添加失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "商品添加失败: " + e.getMessage());
        }
    }

    // 更新商品
    @PutMapping("/update")
    public Result updateGoods(@RequestBody Goods goods) {
        try {
            int result = goodsService.updateGoods(goods);
            if (result > 0) {
                return Result.success("商品更新成功");
            } else {
                return Result.fail(500, "商品更新失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "商品更新失败: " + e.getMessage());
        }
    }

    // 删除商品(软删除)
    @DeleteMapping("/delete/{id}")
    public Result deleteGoods(@PathVariable Integer id) {
        try {
            int result = goodsService.deleteGoods(id);
            if (result > 0) {
                return Result.success("商品删除成功");
            } else {
                return Result.fail(500, "商品删除失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "商品删除失败: " + e.getMessage());
        }
    }

    // 切换商品状态(上下架)
    @PostMapping("/status/{id}")
    public Result reverseStatus(@PathVariable Integer id) {
        try {
            int result = goodsService.reverseStatus(id);
            if (result > 0) {
                Goods goods = goodsService.getGoodsById(id);
                String status = goods.getStatus() == 1 ? "上架" : "下架";
                return Result.success("商品已" + status);
            } else {
                return Result.fail(500, "状态更新失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "状态更新失败: " + e.getMessage());
        }
    }

    // 获取商品列表
    @GetMapping("/list")
    public Result getGoodsList(Goods goods) {
        try {
            return Result.success(goodsService.getAll(goods));
        } catch (Exception e) {
            return Result.fail(500, "获取商品列表失败: " + e.getMessage());
        }
    }

    // 获取商品分页列表
    @GetMapping("/page")
    public Result getGoodsPage(Goods goods, Integer current, Integer size) {
        try {
            IPage<Goods> page = new Page<>(current != null ? current : 1, size != null ? size : 10);
            return Result.success("查询成功", goodsService.getPage(goods, page));
        } catch (Exception e) {
            return Result.fail(500, "获取商品分页列表失败: " + e.getMessage());
        }
    }

    // 根据分类获取商品列表
    @GetMapping("/category/{category}")
    public Result getGoodsByCategory(@PathVariable String category) {
        try {
            List<Goods> goodsList = goodsService.getGoodsByCategory(category);
            return Result.success(goodsList);
        } catch (Exception e) {
            return Result.fail(500, "获取商品列表失败: " + e.getMessage());
        }
    }

}
